package com.google.code.ptrends.common.utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * Helper class for use in unit-tests only
 * 
 * @author ternovykh
 * 
 */
public final class FileHelper {

	private static final Logger LOG = Logger.getLogger(FileHelper.class);

	private FileHelper() {
	}

	/**
	 * Reads line with given index for given file
	 * 
	 * @param fileName
	 * @param index
	 *            Line index. First line in file has index = 0
	 * @return Line at given index if exists, null otherwise
	 */
	public static String readLine(final String fileName, final int index) {
		InputStreamReader input = null;
		BufferedReader reader = null;
		String result = null;
		try {
			input = new InputStreamReader(new FileInputStream(fileName),
					Charset.forName("UTF-8"));
			reader = new BufferedReader(input);

			if (index == 0) {
				result = reader.readLine();
			} else {
				for (int i = 0; i < index; ++i) {
					reader.readLine();
				}
				result = reader.readLine();
			}
		} catch (IOException e) {
			LOG.error("Error reading line", e);
		} finally {
			closeReader(reader);
			closeReader(input);
		}

		return result;
	}

	/**
	 * Reads all lines from given file
	 * 
	 * @param fileName
	 * @return All lines for given file (or empty array if file is empty)
	 * @throws IOException
	 */
	public static String[] readAllLines(final String fileName)
			throws IOException {
		final List<String> result = new ArrayList<String>();
		InputStreamReader input = null;
		BufferedReader reader = null;
		try {
			input = new InputStreamReader(new FileInputStream(fileName),
					Charset.forName("UTF-8"));
			reader = new BufferedReader(input);

			String line = reader.readLine();
			while (line != null) {
				result.add(line);

				line = reader.readLine();
			}
		} finally {
			closeReader(reader);
			closeReader(input);
		}

		return result.toArray(new String[result.size()]);
	}

	/**
	 * Reads all lines from given file as single line
	 * 
	 * @param fileName
	 * @return All lines from given file as one long line
	 * @throws IOException
	 */
	public static String readAll(final String fileName) throws IOException {
		final StringBuilder stringBuilder = new StringBuilder();
		InputStreamReader input = null;
		BufferedReader reader = null;
		try {
			input = new InputStreamReader(new FileInputStream(fileName),
					Charset.forName("UTF-8"));
			reader = new BufferedReader(input);

			String line = reader.readLine();
			while (line != null) {
				stringBuilder.append(line);

				line = reader.readLine();
			}
		} finally {
			closeReader(reader);
			closeReader(input);
		}

		return stringBuilder.toString();
	}

	private static void closeReader(final Reader reader) {
		if (reader == null) {
			return;
		}

		try {
			reader.close();
		} catch (Exception e) {
			LOG.error("Error closing reader", e);
		}
	}
}
